﻿using BLL;
using Models;
using Models.ApiModel;
using Models.ApiModel.req;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using webui.ViewModel;
using WHC.Framework.Commons;
using Tools;
using Models.webReq;
using System.IO;
using System.Configuration;
using System.Web.Hosting;

namespace webui.Controllers
{
    public class ApiAdminController : ApiController
    {
        UserInfoBLL iuserbll = new UserInfoBLL();
        // GET api/<controller>
        [HttpPost]
        public ResUser Login(LoginUser userinfo)
        {
            ResUser res = new ResUser();
            if (userinfo == null || string.IsNullOrEmpty(userinfo.LoginName) || string.IsNullOrEmpty(userinfo.Pwd))
            {
                res.Code = 0;
                res.Msg = "参数不能为空";
                return res;
            }
         
            UserInfo usr = iuserbll.Login(userinfo.LoginName, userinfo.Pwd);
            if (usr != null)
            {
                res.Code = 1;
                UserInfo user2 = iuserbll.GetUserhasRole(usr.id);
                res.RoleName = user2.Rolename;
                res.UserName = usr.username;
                DateTime express = DateTime.Now.AddDays(5);
                long expres = DateTimeHelper.ConvertTimeToJS(express);
                Token tk = new Token()
                {
                    ExprTime = expres,
                    UserName = res.UserName,
                    RoleName = res.RoleName,
                     Userid = user2.id,
                    Shopid=(int)user2.Shopid
                };
                string str = tk.ToJson();
                res.Shopid = (int)usr.Shopid;
                res.Token = Base64Util.Encrypt(str); 

            }
            else
            {
                res.Code = 0;
                res.Msg = "用户名或密码错误";
            }
            return res;
        }

        [HttpPost]
        public ResUser ReferToken(string token)
        {
            ResUser res = new ResUser();
            res.Code = 0;
            if (string.IsNullOrEmpty(token))
            {
                 res.Msg = "参数不能为空";
                return res;
            }
            try
            {
                string userstr = Base64Util.Decrypt(token);
                Token user = userstr.ToObject<Token>();
                if (user == null || string.IsNullOrEmpty(user.RoleName) || user.Userid == 0)
                {
                    res.Msg = "登录参数不合法已过期，请重新登录";
                    return res;
                }
                else
                {
                    long curent = DateTimeHelper.ConvertTimeToJS(DateTime.Now);
                    if (curent > user.ExprTime)
                    {
                        res.Msg = "登录时间已过去";
                        res.Code = 0;
                    }
                    else
                    {
                        //验证时间
                        DateTime express = DateTime.Now.AddDays(5);
                        user.ExprTime = DateTimeHelper.ConvertTimeToJS(express);
                        res.RoleName = user.RoleName;
                        res.UserName = user.UserName;
                        res.Shopid = user.Shopid;
                        UserInfo dbuser = iuserbll.GetById(user.Userid);
                         res.Shopid = (int)dbuser.Shopid;
                        res.Msg = "ok";
                        res.Code = 1;
                        res.Token = Base64Util.Encrypt(user.ToJson()); //更新token
                    }
                }

            }
            catch (Exception ce)
            {
                res.Msg =ce.Message ;
            }
             
            return res;


        }

        //添加建议
        [VerifyKeyAttribute]
        public BaseRes AddProposal(Proposal pro)
        {
            ProposalBLL probll = new ProposalBLL();
            BaseRes res = new BaseRes();
            res.Code = 0;
            if (string.IsNullOrEmpty(pro.Msg))
            {
                res.Msg = "留言不能为空";
                return res;
            }
            if (string.IsNullOrEmpty(pro.Phone))
            {
                res.Msg = "联系方式不能为空";
                return res;
            }
           
            string userstr = Base64Util.Decrypt(pro.Token);
            Token user = userstr.ToObject<Token>();
            pro.UserName = user.UserName + user.Userid;
            pro.Createtime = DateTime.Now;
            pro.Caclstate = 0;
            probll.Insert(pro);
            res.Code = 1;
            res.Msg = "已收到您的建议，我们会认真考虑";
            return res;
        }

        public BaseRes Update(AppUpdate appinfo)
        {
            var url = HostingEnvironment.MapPath("~/");
            BaseRes res = new BaseRes();
            res.Code = 0;
            if ( string.IsNullOrEmpty(appinfo.versionCode))
            {
                res.Msg = "客户端版本号错误";
            }
            int appnum =  WHC.Framework.Commons.ConvertHelper.ToInt32(appinfo.versionCode,0);
            if (appnum==0)
            {
                res.Msg = "客户端版本号0";
            }
            string txtfile= Path.Combine(url,"update.txt"); ;
            string[] sarr=  File.ReadAllLines(txtfile);
            if (!string.IsNullOrEmpty(sarr[0]))
            {
                string[] arrVserion = sarr[0].Split('=');
                if (arrVserion.Length != 2)
                {
                    res.Msg = "更新列表错误";
                    return res;
                }
                else
                {
                   int maxNum = int.Parse(arrVserion[1]);
                    if (maxNum> appnum)
                    {
                        //需要升级
                        string host = ConfigurationSettings.AppSettings["httpip"];
                        res.Data = host + "esc" + maxNum + ".apk";
                        res.Code = 1;
                        res.Msg = "发现新的版本，为了更好的体验请更新APP";
                    }
                }
            }
            else
            {
                res.Msg = "配置错误";
            }
            return res;
              
            
        }



    }
}